{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7e0a3069",
   "metadata": {},
   "source": [
    "* 2022/10/11/week08\n",
    "* 课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f933bd0f",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "> 代码的注释  \n",
    "> 用户输入  \n",
    "> 格式化输出  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c85f0cb6",
   "metadata": {},
   "source": [
    "### 代码的注释\n",
    "* 1.用 # 注释，一般放在代码块的最上边，或者每一块代码的最后，用于代码思路的记录  \n",
    "* 2.'''注释内容''' 一般是注释代码块  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc807ba7",
   "metadata": {},
   "source": [
    "### 1.用户输入\n",
    "* 1.input()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab6c7f0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ABbbbb\n",
      "密码:123456\n",
      "ABbbbb\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登录登录\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "89077467",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ABbbbb\n",
      "密码:1234567\n",
      "账号不存在或者密码不正确\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了判断功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "#情景一：输入信息征求，登陆成功\n",
    "if userl == username and password1 == password:\n",
    "    print(\"欢迎\"+userl+\"登录！\")\n",
    "#情景二：帐号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或者密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39523ec5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ABbbbb\n",
      "密码:12345678\n",
      "账号不存在或者密码不正确\n",
      "账号:ABbbbb\n",
      "密码:1234567\n",
      "账号不存在或者密码不正确\n",
      "账号:ABbbbb\n",
      "密码:123456\n",
      "欢迎ABbbbb登录！\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了循环功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3):\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息征求，登陆成功\n",
    "    if userl == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录！\")\n",
    "    #情景二：帐号不存在或密码不正确\n",
    "    else:\n",
    "        print(\"账号不存在或者密码不正确\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d8b970a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ABbbbb\n",
      "密码:1234567\n",
      "账号不存在或者密码不正确\n",
      "账号:ABbbbb\n",
      "密码:123456\n",
      "欢迎ABbbbb登录！\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Input \u001b[1;32mIn [5]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m password1 \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m123456\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m3\u001b[39m):\n\u001b[1;32m----> 7\u001b[0m     username \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m账号:\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m      8\u001b[0m     password \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m密码:\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     10\u001b[0m     \u001b[38;5;66;03m#情景一：输入信息征求，登陆成功\u001b[39;00m\n",
      "File \u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py:1075\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m   1071\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_allow_stdin:\n\u001b[0;32m   1072\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(\n\u001b[0;32m   1073\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m   1074\u001b[0m     )\n\u001b[1;32m-> 1075\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   1076\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1077\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1078\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mshell\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m   1079\u001b[0m \u001b[43m    \u001b[49m\u001b[43mpassword\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m   1080\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py:1120\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m   1117\u001b[0m             \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[0;32m   1118\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[0;32m   1119\u001b[0m     \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[1;32m-> 1120\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInterrupted by user\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28mNone\u001b[39m\n\u001b[0;32m   1121\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[0;32m   1122\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlog\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid Message:\u001b[39m\u001b[38;5;124m\"\u001b[39m, exc_info\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了循环功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3):\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息征求，登陆成功\n",
    "    if userl == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录！\")\n",
    "    #情景二：帐号不存在或密码不正确\n",
    "    else:\n",
    "        print(\"账号不存在或者密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2302f2e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环三次，但是我第二次的时候已经填对密码了，但是for循环还没结束，现在完善，当账号密码正确时，结束循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a7f510f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 登录系统 迭代1：增加了循环功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息征求，登陆成功\n",
    "    if userl == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录！\")\n",
    "    #情景二：帐号不存在或密码不正确\n",
    "    else:\n",
    "        print(\"账号不存在或者密码不正确\")\n",
    "        print(\"您还有\"+i+\"次尝试的机会\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "458d1c4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:ABbbbb\n",
      "密码:1234567\n",
      "账号不存在或者密码不正确\n",
      "您还有2次尝试的机会\n",
      "账号:ABbbbb\n",
      "密码:123456\n",
      "欢迎ABbbbb登录！\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了循环功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 是一个 int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息正确，登陆成功\n",
    "    if userl == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录！\")\n",
    "        break # 终止循环\n",
    "    #情景二：帐号不存在或密码不正确\n",
    "    else:\n",
    "        print(\"账号不存在或者密码不正确\")\n",
    "        print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6f7d116b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:dfs\n",
      "密码:fsfg\n",
      "账号不存在或者密码不正确\n",
      "您还有2次尝试的机会\n",
      "账号:ffg\n",
      "密码:sdfg\n",
      "账号不存在或者密码不正确\n",
      "您还有1次尝试的机会\n",
      "账号:sfg\n",
      "密码:sdfg\n",
      "三次输入失败，请在一小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 登录系统 迭代1：增加了循环功能\n",
    "# 假设我们数据库有用户信息：账号：ABbbbb ；密码：123456\n",
    "userl = \"ABbbbb\"\n",
    "password1 = \"123456\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 是一个 int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    #情景一：输入信息正确，登陆成功\n",
    "    if userl == username and password1 == password:\n",
    "        print(\"欢迎\"+userl+\"登录！\")\n",
    "        break # 终止循环\n",
    "    #情景二：帐号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请在一小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或者密码不正确\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "623fc9c0",
   "metadata": {},
   "source": [
    "## 格式化输出\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "13d74a7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:ABbbb\n",
      "年龄:20\n",
      "工作:工程师 \n",
      "收入:100000\n",
      "地区:泰国\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：用字符串拼接\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''-----'''+'''\n",
    "年龄:'''+age+'''\n",
    "工作:'''+job+'''\n",
    "收入:'''+salary+'''\n",
    "地区:'''+reg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1ceb70ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:hdf\n",
      "年龄:adfhaiurg\n",
      "工作:addgh\n",
      "收入:agae\n",
      "地区:afgag\n",
      "-----INFO OF name-----\n",
      "年龄:age\n",
      "工作:job\n",
      "收入:salary\n",
      "地区:reg\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''-----INFO OF name-----\n",
    "年龄:age\n",
    "工作:job\n",
    "收入:salary\n",
    "地区:reg'''\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2d28a8a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:张真源\n",
      "年龄:19\n",
      "工作:学生\n",
      "收入:10000\n",
      "地区:重庆\n",
      "-----INFO OF 张真源-----\n",
      "年龄:19\n",
      "工作:学生\n",
      "收入:10000\n",
      "地区:重庆\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 &s str 依次有序输入\n",
    "name = input(\"姓名:\")\n",
    "age = input(\"年龄:\")\n",
    "job = input(\"工作:\")\n",
    "salary = input(\"收入:\")\n",
    "reg = input(\"地区:\")\n",
    "\n",
    "info = '''-----INFO OF %s-----\n",
    "年龄:%s\n",
    "工作:%s\n",
    "收入:%s\n",
    "地区:%s'''%(name,age,job,salary,reg)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e5a1911",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "* 要求\n",
    " > 1.运行程序后，让用户输入支付余额，然后打印我们的商品列表给用户    \n",
    " > 2.让用户输入商品编号进行商品的购买    \n",
    " > 3.用户选择商品后，检查用户的余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bffa6ae5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
